##################
# Angel M. Villegas-Cruz
# This file replicates the heatmaps, namely Figure 3, and Appendix Figures 3 and 4.
# 08/18/2024
##################

library(rworldmap)
library(tidyverse)
library(countrycode)

#Run "Replication - Main results.R"

###########################
#Figure 3: Chinese diplomatic missions using Twitter by country (Main manuscript)
###########################

#Create new dataset with only country and twitter handdle (i.e., screen name)
x = as.data.frame(cbind(mydata$Country,mydata$screen_name))
names(x)=c("country",'screen_name')
x2 = x %>% distinct()
x3 = x2 %>% group_by(country) %>% mutate(count = n())
x4 = x3 %>% distinct(country, count)

x4$ccode <- countrycode(x4$country, origin = "country.name", destination = "iso3c") 

#ISO3 is the character code, not numeric code
mapped_data <- joinCountryData2Map(x4, joinCode = "ISO3",
                                   nameJoinColumn="ccode")

#Include all Chinese diplomatic missions using Twitter by country
dev.new()
par(mai=c(0,0,0.2,0),xaxs="i",yaxs="i")
mapCountryData(mapped_data, nameColumnToPlot = "count",
               mapTitle="",
               catMethod=c(1:4),
               colourPalette = "white2Black")

#Save as "Figure 3.pdf"

###########################
##Appendix Figures 3 and 4
#Two maps to visualize the spatial distribution of two dependent variables, respectively.
###########################

#Create dataset for Figure 3
x = mydata %>% dplyr::select(Country, threat, bene_rule)

x$cat_bene_rule = ifelse(x$bene_rule==1, "Benevolent Rule", NA)
x$cat_threat = ifelse(x$threat==1, "Threat", NA)

x = x %>% distinct(Country, threat, bene_rule, .keep_all = TRUE) #remove duplicates

x$ccode <- countrycode(x$Country, origin = "country.name", destination = "iso3c") 

#Only include overseas missions using the threat strategy
x <- subset(x, is.na(cat_threat)==F)

#ISO3 is the character code, not numeric code
mapped_data <- joinCountryData2Map(x, 
                                   joinCode = "ISO3",
                                   nameJoinColumn="ccode")

#Figure 3: Chinese diplomatic missions using the threat strategy by country
dev.new()
par(mai=c(0,0,0.2,0),xaxs="i",yaxs="i")
mapCountryData(mapped_data, 
               nameColumnToPlot = "cat_threat",
               mapTitle="",
               colourPalette = "white2Black",
               catMethod="categorical",
               mapRegion=("World"),
               borderCol="black")

#Save as "Appendix Figure 3.pdf"

#Create dataset for Figure 4
x = mydata %>% dplyr::select(Country, threat, bene_rule)

x$cat_bene_rule = ifelse(x$bene_rule==1, "Benevolent Rule", NA)
x$cat_threat = ifelse(x$threat==1, "Threat", NA)

x = x %>% distinct(Country, threat, bene_rule, .keep_all = TRUE) #remove duplicates

x$ccode <- countrycode(x$Country, origin = "country.name", destination = "iso3c") 

#Only include overseas missions using the benevolent rule strategy
x <- subset(x, is.na(cat_bene_rule)==F)

#ISO3 is the character code, not numeric code
mapped_data <- joinCountryData2Map(x, 
                                   joinCode = "ISO3",
                                   nameJoinColumn="ccode")

#Figure 4: Chinese diplomatic missions using the benevolent rule strategy by country
dev.new()
par(mai=c(0,0,0.2,0),xaxs="i",yaxs="i")
mapCountryData(mapped_data, 
               nameColumnToPlot = "cat_bene_rule",
               mapTitle="",
               colourPalette = "white2Black",
               catMethod="categorical",
               mapRegion=("World"),
               borderCol="black")

#Save as "Appendix Figure 4.pdf"